<template>
  <div class="app-container">
    <el-select v-model="value" placeholder="选择服务端路径" @change="changePath">
      <el-option v-for="item in paths" :key="item.path" :label="item.path" :value="item.path"></el-option>
    </el-select>
    <el-button type="primary" @click="openpathdialog">添加路径</el-button>
    <el-button type="danger" @click="delmirpath">删除路径</el-button>
    <el-button type="success" @click="loadmirinfo">读取信息</el-button>
    <el-container v-if="config!=''" class="app-container">
      <el-header>
        <el-button type="success" @click="startm2" v-if="activeinfo">启动引擎</el-button>
        <el-button type="success" @click="cloneDialogVisible = true" v-if="!activeinfo">克隆引擎</el-button>
      </el-header>

      <el-main v-if="activeinfo">
        <el-collapse>
          <el-collapse-item title="服务端信息" name="1">
            <el-collapse v-model="activeCollapse">
              <el-collapse-item title="基本信息" name="1">
                <div>服务器名：{{config.Gamename}}</div>
                <div>服务器路径：{{config.Path}}</div>
                <div>服务器IP：{{config.IP}}</div>
              </el-collapse-item>
              <el-collapse-item title="登录网关 LoginGate" name="2">
                <div>端口：{{config.LoginGateGatePort}}</div>
              </el-collapse-item>
              <el-collapse-item title="登录服务器 LoginServer" name="3">
                <div>连接端口：{{config.LoginServerGatePort}}</div>
                <div>通讯端口：{{config.LoginServerServerPort}}</div>
              </el-collapse-item>
              <el-collapse-item title="角色网关 SelGate" name="4">
                <div>端口：{{config.SelGateGatePort}}</div>
              </el-collapse-item>
              <el-collapse-item title="数据库服务器 DBServer" name="4">
                <div>连接端口：{{config.DBGatePort}}</div>
                <div>通讯端口：{{config.DBServerPort}}</div>
              </el-collapse-item>
              <el-collapse-item title="M2服务器 M2Server" name="4">
                <div>连接端口：{{config.M2GatePort}}</div>
                <div>通讯端口：{{config.M2MsgSrvPort}}</div>
              </el-collapse-item>
              <el-collapse-item title="游戏网关 RunGate" name="4">
                <div>网关开启数量：{{config.RGCount}}</div>
                <div>网关1端口：{{config.RGGatePort1}}</div>
                <div>网关2端口：{{config.RGGatePort2}}</div>
                <div>网关3端口：{{config.RGGatePort3}}</div>
                <div>网关4端口：{{config.RGGatePort4}}</div>
                <div>网关5端口：{{config.RGGatePort5}}</div>
                <div>网关6端口：{{config.RGGatePort6}}</div>
                <div>网关7端口：{{config.RGGatePort7}}</div>
                <div>网关8端口：{{config.RGGatePort8}}</div>
              </el-collapse-item>
              <el-collapse-item title="日志服务器 LogServer" name="4">
                <div>端口：{{config.LogServerPort}}</div>
              </el-collapse-item>
            </el-collapse>
          </el-collapse-item>
        </el-collapse>
      </el-main>
    </el-container>

    <el-dialog title="提示" :visible.sync="cloneDialogVisible" width="30%" center>
      <div>
        选择源：
        <el-select v-model="clonepath" placeholder="选择服务端路径">
          <el-option v-for="item in paths" :key="item.path" :label="item.path" :value="item.path"></el-option>
        </el-select>
      </div>
      <div>
        服务器名：
        <el-input placeholder="服务器名" v-model="mirname" clearable></el-input>
      </div>
      <div>
        端口号增加：
        <el-input-number v-model="num" :min="0" :max="10" label="端口号增加"></el-input-number>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cloneDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="clonem2">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog title="提示" :visible.sync="pathDialogVisible" width="30%" center>
      服务端路径：
      <el-input placeholder="请输入内容" v-model="path" clearable></el-input>
      <span slot="footer" class="dialog-footer">
        <el-button @click="pathDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addpath">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import * as API from "@/api/remote";
import { config } from "@vue/test-utils";
export default {
  data() {
    return {
      id: this.$route.params.id,
      skey: this.$route.params.skey,
      paths: [],
      path: "",
      clonepath: "",
      value: "",
      pathDialogVisible: false,
      key: "",
      file: "",
      title: "",
      reload: false,
      config: [],
      activeinfo: false,
      activeCollapse: ["1", "2", "3", "4", "5"],
      loadres: false,
      cloneDialogVisible: false,
      clonepath: "",
      num: 0,
      mirname: ""
    };
  },

  created() {
    if (this.$route.params.id == null) {
      this.$notify({
        title: "错误",
        message: "未选择网关",
        type: "warning",
        duration: 2000
      });
      this.$router.push({ name: "clients" });
      return;
    }
    this.getmirpath();
  },
  methods: {
    clonem2() {
      if (this.clonepath == this.path) {
        this.$notify({
          title: "错误",
          message: "源路径不能为当前路径",
          type: "warning",
          duration: 2000
        });
        return;
      }
      if (this.mirname == "") {
        this.$notify({
          title: "错误",
          message: "服务器名字不能为空",
          type: "warning",
          duration: 2000
        });
        return;
      }
      if (this.num == 0) {
        this.$notify({
          title: "错误",
          message: "端口号增加不能为0",
          type: "warning",
          duration: 2000
        });
        return;
      }
      let params = {
        path: this.path,
        copy: this.clonepath,
        plus: this.num,
        name: this.mirname
      };
      API.startm2(this.id,params).then(res=>{
        const data=res.data
        if (data.code == 0) {
          this.$notify({
            title: "成功",
            message: data.msg,
            type: "success",
            duration: 2000
          });
        } else {
          this.$message.error(data.msg);
        }
        this.cloneDialogVisible=false
        this.clonepath=""
        this.num=0
        this.mirname=""
         this.$notify({
            title: "指令发送成功",
            message: "耐心等待，后再读取信息",
            type: "success",
            duration: 2000
          });
      })
    },
    startm2() {
      if (this.path == "") {
        this.$notify({
          title: "错误",
          message: "未选择路径",
          type: "warning",
          duration: 2000
        });
        return;
      }
      API.startm2(this.id, { path: this.value }).then(res => {
        const data = res.data;
        if (data.code == 0) {
          this.$notify({
            title: "成功",
            message: data.msg,
            type: "success",
            duration: 2000
          });
        } else {
          this.$message.error(data.msg);
        }
      });
    },
    loadmirinfo() {
      this.activeinfo = false;
      this.config = [];
      if (this.value == "") {
        this.$notify({
          title: "错误",
          message: "未选择路径",
          type: "warning",
          duration: 2000
        });
        return;
      }
      API.LoadMirInfo(this.id, { path: this.value }).then(res => {
        const data = res.data;
        if (data.code == 0) {
          this.$notify({
            title: "成功",
            message: "获取成功",
            type: "success",
            duration: 2000
          });
          this.config = data.data;
          this.activeinfo = true;
        } else {
          this.config = [1];
          this.loadres = true;
          this.$message.error(data.msg);
        }
      });
    },
    delmirpath() {
      if (this.path == "") {
        this.$notify({
          title: "错误",
          message: "未选择路径",
          type: "warning",
          duration: 2000
        });
        return;
      }
      let params = {
        path: this.path
      };
      API.DelMirPath(this.id, params).then(res => {
        const data = res.data;
        C;
        this.getmirpath();
      });
    },
    changePath(v) {
      this.path = v;
    },
    addpath() {
      if (this.path == "") {
        this.$notify({
          title: "错误",
          message: "未输入信息",
          type: "error",
          duration: 2000
        });
        return;
      }
      let params = { path: this.path };
      API.AddMirPath(this.id, params).then(res => {
        const data = res.data;
        if (data.code == 0) {
          this.$notify({
            title: "成功",
            message: "添加成功",
            type: "success",
            duration: 2000
          });
        } else {
          this.$message.error(data.msg);
        }
        this.pathDialogVisible = false;
        this.getmirpath();
      });
    },
    openpathdialog() {
      (this.path = ""), (this.pathDialogVisible = true);
    },
    getmirpath() {
      API.GetMirPath(this.id).then(response => {
        const data = response.data;
        let arr = data.data.split("|");
        this.paths = [];
        for (let k in arr) {
          if (arr[k] == "") {
            continue;
          }
          this.paths.push({ id: k, path: arr[k] });
        }
      });
    },

    refreshView() {
      // this.getfiles();
    }
  }
};
</script>

<style>
</style>